package com.demo.salary.transactionimplementation;

import com.demo.salary.payrolldomain.Employee;
import com.demo.salary.payrolldomain.PaymentClassification;
import com.demo.salary.payrolldatabase.PayrollDatabase;
import com.demo.salary.transactionapplication.Transaction;
import com.demo.salary.payrollimplementation.CommissionedClassification;
import com.demo.salary.payrollimplementation.SalesReceipt;

import java.time.LocalDate;

/**
 * 销售提成
 */
public class SalesReceiptTransaction implements Transaction {

    private final int empId;
    private final LocalDate date;
    private final double amount;

    public SalesReceiptTransaction(int empId, LocalDate date, double amount) {
        this.empId = empId;
        this.date = date;
        this.amount = amount;
    }

    @Override
    public void execute() {
        Employee employee = PayrollDatabase.getEmployee(empId);
        if (employee == null) {
            throw new RuntimeException("没有该员工");
        }
        PaymentClassification classification = employee.getClassification();
        if (!(classification instanceof CommissionedClassification)) {
            throw new RuntimeException("该员工不是销售");
        }
        CommissionedClassification cc = (CommissionedClassification) classification;
        cc.addSalesReceipt(new SalesReceipt(date, amount));

    }
}
